package com.ytjj.qmyx.mall.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.common.api.ResultCode;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.mall.model.request.PageRequest;
import com.ytjj.qmyx.mall.model.response.LiveInfoResponse;
import com.ytjj.qmyx.mall.service.LiveService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description 直播
 * @ClassName LiveController
 * 
 *
 * @Version 1.0
 **/
@RestController
@RequestMapping("/live")
public class LiveController extends BaseController {

    @Resource
    private LiveService liveService;

    @Resource
    private RedisService redisService;


    /**
     * @Description 获取直播间列表
     * 
     *
     * @Version 1.0
     **/
    @GetMapping("/getLiveRoomList")
    public CommonResult getLiveRoomList(@RequestBody PageRequest pageRequest) {
        return CommonResult.success(liveService.getLiveRoomList(pageRequest));
    }

    /**
     * @Description 获取直播间外观
     *
     * 
     *
     * @Version 1.0
     **/
    @GetMapping("/getLiveRoomInfo")
    public CommonResult getLiveRoomInfo(Integer id) {
        UsersResponse currentUser = getCurrentUser();
        return CommonResult.success(liveService.getLiveRoomInfo(id, currentUser));
    }

    /**
     * @Description 获取直播间商品信息
     *
     * 
     *
     * @Version 1.0
     **/
    @GetMapping("/getLiveRoomProducts")
    public CommonResult getLiveRoomProducts(Integer id) {
        UsersResponse currentUser = getCurrentUser();
        return CommonResult.success(liveService.getLiveRoomProducts(id, currentUser));
    }

    /**
     * @Description 获取正在讲解的商品
     *
     * 
     *
     * @Version 1.0
     **/
    @GetMapping("/getExplainProduct")
    public CommonResult getExplainProduct(Integer id) {
        UsersResponse currentUser = getCurrentUser();
        return CommonResult.success(liveService.getExplainProduct(id));
    }


    /**
     * @Description 主播创建房间
     * 
     *
     * @Version 1.0
     **/
    @GetMapping("/generateLiveRoom")
    public CommonResult generateLiveRoom() {
        UsersResponse currentUser = getCurrentUser();
        if (currentUser.getIsOpenLive() == 0) {
            return CommonResult.success("对不起, 您暂未权限开通直播!");
        }
        // 获取用户数据
        LiveInfoResponse liveInfo = liveService.getLiveInfo(currentUser.getId());
        if (liveInfo == null && currentUser.getIsOpenLive() == 1) {
            liveInfo = liveService.createLiveRoom(currentUser);
        } else {
            liveInfo = liveService.updateLiveRoom(currentUser);
        }
        return CommonResult.success(liveInfo);
    }



    @GetMapping("/getLiveOn")
    public CommonResult getLiveOn() {
        return CommonResult.success(liveService.getLiveOn());
    }


    @GetMapping("/updateLiveRoom")
    public CommonResult updateLiveRoom() {
        UsersResponse currentUser = getCurrentUser();
        return CommonResult.success(liveService.updateLiveRoom(currentUser));
    }

    /**
     * 直播主动关闭房间
     *
     * @param id
     * @return
     */
    @GetMapping("/closeLiveRoom")
    public CommonResult quitLiveRoom(@RequestParam("id") Integer id) {
        liveService.closeLiveRoom(id);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 获取直播间所有信息
     */
    @GetMapping("/getLiveRoomInfos")
    public CommonResult getLoginToken(@RequestParam("id") Integer id) {
       return CommonResult.success(liveService.getLiveRoomInfos(id, getCurrentUser()));
    }






}
